package com.common.www.service.programme;


import org.springframework.boot.base.jpa.service.BaseLogicService;
import com.common.www.bean.entity.programme.*;
import com.common.www.bean.entity.project.SupplyCompany;
import com.common.www.repository.programme.PrecipitateDbRepository;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import static com.common.www.service.tabulation.InnerTabulationService.createCellAddStype;


@Service
@Transactional
public class PrecipitateDbService extends BaseLogicService<PrecipitateDb, Long> {
    @Autowired
    private PrecipitateDbRepository repository;

    @Override
    public PrecipitateDbRepository getRepository() {
        return repository;
    }


    /**
     * 组装定标沉淀数据
     * @param dbList
     * @param conditionType 1：查全部，2.只查合计
     * @return
     */
    public List<Map<String, Object>> getAssembleDbList(List<PrecipitateDb> dbList,Integer conditionType) {
        //按供应商分组
        List<Map<String, Object>> mapList = new ArrayList<>();
        Map<Long, List<PrecipitateDb>> suNameList = dbList.stream().collect(Collectors.groupingBy(vo -> vo.getSupplyCompany().getId()));
        int index = 0;
        for (Map.Entry<Long, List<PrecipitateDb>> sItem : suNameList.entrySet()) {
            index++;
            List<PrecipitateDb> tempList = new ArrayList<>();
            if (conditionType != null && conditionType == 2){
                for (PrecipitateDb item : sItem.getValue()){
                    if (item.getFenXiType() == 1 || item.getFenXiType() == 2 || item.getFenXiType() == 3 || item.getFenXiType() == 4 || item.getFenXiType() == 9 ||
                            item.getFenXiType() == 14 || item.getFenXiType() == 19 || item.getFenXiType() == 24 || item.getFenXiType() == 29 || item.getFenXiType() == 30 ||
                            item.getFenXiType() == 31 || item.getFenXiType() == 32 || item.getFenXiType() == 33 || item.getFenXiType() == 34 || item.getFenXiType() == 35){
                        tempList.add(item);
                    }
                }
            }else {
                tempList = sItem.getValue();
            }
            //固定排序
            List<PrecipitateDb> sortedList = tempList.stream()
                    .sorted(Comparator.comparing(PrecipitateDb::getFenXiType))
                    .collect(Collectors.toList());
            Map<String, Object> tempMap = new HashMap<>();
            tempMap.put("count", index);
            tempMap.put("supplyCompany", sortedList.get(0).getSupplyCompany());
            tempMap.put("dataChenDian", sortedList);
            mapList.add(tempMap);
        }
        return mapList;
    }


    /**
     * 导出定标阶段数据沉淀
     *
     * @param mapList
     * @return
     */
    public XSSFWorkbook exportDb(List<Map<String, Object>> mapList) {
        XSSFWorkbook workbook = new XSSFWorkbook();
        for (int i = 0; i < mapList.size(); i++) {
            Map<String, Object> map = mapList.get(i);
            Object supplyCompanyObj = map.get("supplyCompany");
            String sheetName = "";
            if (supplyCompanyObj instanceof SupplyCompany) { // 假设对象类型是 SupplyCompany
                SupplyCompany supplyCompany = (SupplyCompany) supplyCompanyObj;
                sheetName = supplyCompany.getName();
            }
            Sheet sheet = workbook.createSheet(sheetName);
            CellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setAlignment(HorizontalAlignment.CENTER); // 居中
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 居中
            // 创建日期格式
            CellStyle cellDateStyle = workbook.createCellStyle();
            CreationHelper createHelper = workbook.getCreationHelper();
            cellDateStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-mm-dd hh:mm:ss"));
            Row row0 = sheet.createRow(0);
            createCellAddStype(row0, 0, cellStyle).setCellValue("定标-高低压工程定标分析（数据分析）");

            CellRangeAddress titleRegion = new CellRangeAddress(0, 0, 0, 13);// 下标从0开始 起始行号，终止行号， 起始列号，终
            sheet.addMergedRegionUnsafe(titleRegion);
            //边框
            cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
            cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
            cellStyle.setBorderTop(BorderStyle.THIN);//上边框
            cellStyle.setBorderRight(BorderStyle.THIN);//右边框
            cellDateStyle.setBorderBottom(BorderStyle.THIN); //下边框
            cellDateStyle.setBorderLeft(BorderStyle.THIN);//左边框
            cellDateStyle.setBorderTop(BorderStyle.THIN);//上边框
            cellDateStyle.setBorderRight(BorderStyle.THIN);//右边框

            // 生成表头
            Row row1 = sheet.createRow(1);
            createCellAddStype(row1, 0, cellStyle).setCellValue("分类");
            createCellAddStype(row1, 1, cellStyle).setCellValue("标底A");
            createCellAddStype(row1, 2, cellStyle).setCellValue("标底A");
            createCellAddStype(row1, 3, cellStyle).setCellValue("标底A");
            createCellAddStype(row1, 4, cellStyle).setCellValue("标底B");
            createCellAddStype(row1, 5, cellStyle).setCellValue("标底B");
            createCellAddStype(row1, 6, cellStyle).setCellValue("标底B");
            createCellAddStype(row1, 7, cellStyle).setCellValue("同城市历史项目数据C");
            createCellAddStype(row1, 8, cellStyle).setCellValue("同城市历史项目数据C");
            createCellAddStype(row1, 9, cellStyle).setCellValue("同城市历史项目数据C");
            createCellAddStype(row1, 10, cellStyle).setCellValue("拟定标-标底差异 B-A");
            createCellAddStype(row1, 11, cellStyle).setCellValue("拟定标-标底差异 B-A");
            createCellAddStype(row1, 12, cellStyle).setCellValue("拟定标-同城市历史项目差异 B-A");
            createCellAddStype(row1, 13, cellStyle).setCellValue("拟定标-同城市历史项目差异 B-A");

            // 生成第二行
            Row row2 = sheet.createRow(2);
            createCellAddStype(row2, 0, cellStyle).setCellValue("分类");
            createCellAddStype(row2, 1, cellStyle).setCellValue("标底价(元)");
            createCellAddStype(row2, 2, cellStyle).setCellValue("建面单方(元/㎡)");
            createCellAddStype(row2, 3, cellStyle).setCellValue("关键单方(元/㎡)");
            createCellAddStype(row2, 4, cellStyle).setCellValue("定标价(元)");
            createCellAddStype(row2, 5, cellStyle).setCellValue("建面单方(元/㎡)");
            createCellAddStype(row2, 6, cellStyle).setCellValue("关键单方(元/㎡)");
            createCellAddStype(row2, 7, cellStyle).setCellValue("实施方案(审核价)");
            createCellAddStype(row2, 8, cellStyle).setCellValue("建面单方(元/㎡)");
            createCellAddStype(row2, 9, cellStyle).setCellValue("关键单方(元/㎡)");
            createCellAddStype(row2, 10, cellStyle).setCellValue("建面单方(元/㎡)");
            createCellAddStype(row2, 11, cellStyle).setCellValue("差异原因分析");
            createCellAddStype(row2, 12, cellStyle).setCellValue("建面单方(元/㎡)");
            createCellAddStype(row2, 13, cellStyle).setCellValue("差异原因分析");

            CellRangeAddress cra1 = new CellRangeAddress(1, 2, 0, 0);
            CellRangeAddress cra2 = new CellRangeAddress(1, 1, 1, 3);
            CellRangeAddress cra3 = new CellRangeAddress(1, 1, 4, 6);
            CellRangeAddress cra4 = new CellRangeAddress(1, 1, 7, 9);
            CellRangeAddress cra5 = new CellRangeAddress(1, 1, 10, 11);
            CellRangeAddress cra6 = new CellRangeAddress(1, 1, 12, 13);
            //在sheet里增加合并单元格
            sheet.addMergedRegion(cra1);
            sheet.addMergedRegion(cra2);
            sheet.addMergedRegion(cra3);
            sheet.addMergedRegion(cra4);
            sheet.addMergedRegion(cra5);
            sheet.addMergedRegion(cra6);

            // 生成数据行
            Integer rowIndex = 3;
            Collection<?> sValueCollection = (Collection<?>) mapList.get(i).get("dataChenDian");
            for (Object sValueObj : sValueCollection) {
                // sValueObj 是自定义对象
                if (sValueObj instanceof PrecipitateDb) {
                    PrecipitateDb item = (PrecipitateDb) sValueObj;
                    Row row = sheet.createRow(rowIndex);
                    createCellAddStype(row, 0, cellStyle).setCellValue(item.getFenXiTypeName());
                    createCellAddStype(row, 1, cellStyle).setCellValue(item.getBasePriceA() != null ? item.getBasePriceA().toString() : "0");
                    createCellAddStype(row, 2, cellStyle).setCellValue(item.getBasePriceAjm() != null ? item.getBasePriceAjm().toString() : "0");
                    createCellAddStype(row, 3, cellStyle).setCellValue(item.getBasePriceAgj() != null ? item.getBasePriceAgj().toString() : "0");
                    createCellAddStype(row, 4, cellStyle).setCellValue(item.getDingBiaoPriceB() != null ? item.getDingBiaoPriceB().toString() : "0");
                    createCellAddStype(row, 5, cellStyle).setCellValue(item.getDingBiaoPriceBjm() != null ? item.getDingBiaoPriceBjm().toString() : "0");
                    createCellAddStype(row, 6, cellStyle).setCellValue(item.getDingBiaoPriceBgj() != null ? item.getDingBiaoPriceBgj().toString() : "0");
                    createCellAddStype(row, 7, cellStyle).setCellValue(item.getChoiceBasePriceC() != null ? item.getChoiceBasePriceC().toString() : "0");
                    createCellAddStype(row, 8, cellStyle).setCellValue(item.getChoiceBasePriceCjm() != null ? item.getChoiceBasePriceCjm().toString() : "0");
                    createCellAddStype(row, 9, cellStyle).setCellValue(item.getChoiceBasePriceCgj() != null ? item.getChoiceBasePriceCgj().toString() : "0");
                    createCellAddStype(row, 10, cellStyle).setCellValue(item.getBaPriceJm() != null ? item.getBaPriceJm().toString() : "0");
                    createCellAddStype(row, 11, cellStyle).setCellValue(item.getBaPriceCysm());
                    createCellAddStype(row, 12, cellStyle).setCellValue(item.getBcPriceJm() != null ? item.getBcPriceJm().toString() : "0");
                    createCellAddStype(row, 13, cellStyle).setCellValue(item.getBcPriceCysm());
                    rowIndex++;
                }
            }
        }
        return workbook;
    }


}
